local DIY = require "packages.diy_utility.diy_utility"

local zhanwang = fk.CreateSkill {
  name = "rmt__zhanwang",
  dynamic_desc = function(self, player)
  local info = ""
  for loop = 1, 30, 1 do
    local mark = player:getMark("rmt__zhanwang_area"..loop)
    if mark ~= 0 then
      if info == "" then
        info = loop.."."..Fk:translate(mark)..";"
      else
        info = info.." "..loop.."."..Fk:translate(mark)..";"
      end
    end
  end
  if info == "" then return ":rmt__zhanwang" end
    return "当你弃置所有手牌后，失去1点体力并从以下处各获得一张牌："..info.." 当你造成伤害后，可以将其中一项改为受伤角色的名字（不可重复）。"
  end,
}

Fk:loadTranslationTable{
  ["rmt__zhanwang"] = "谵妄",
  [":rmt__zhanwang"] = "当你弃置所有手牌后，失去1点体力并从以下处各获得一张牌：①中央区；②牌堆顶；③场上；当你造成伤害后，可以将其中一项改为受伤角色的名字。（不可重复）",

  ["CenterArea"] = "中央区",
  ["DrawPileTop"] = "牌堆顶",
  ["CardInBoard"] = "场上",
  ["Blank"] = "空白",
  ["#rmt__zhanwang_getCenter"] = "谵妄：请获得中央区一张牌！",
  ["#rmt__zhanwang_getInBoard"] = "谵妄：请选择场上有牌的一名角色，请选择其的一张牌获得！",
  ["#rmt__zhanwang_getPlayer"] = "谵妄：请选择%dest区域内的一张牌获得！",
  ["#rmt__zhanwang-choice"] = "谵妄：请选择一个选项改为%dest的名字！",
}

zhanwang:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(zhanwang.name) then return false end
    if event == fk.AfterCardsMove and player:isKongcheng() then
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            return info.fromArea == Card.PlayerHand
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, zhanwang.name)
    if player.dead then return false end
    for loop = 1, 30, 1 do
      local mark = player:getMark("rmt__zhanwang_area"..loop)
      if mark ~= 0 and mark ~= "Blank" then
        if mark == "CenterArea" and player.room:getBanner("@$CenterArea") ~= nil and #player.room:getBanner("@$CenterArea") > 0 then
          local get = room:askToChooseCards(player, {
            target = player,
            flag = { card_data = { {"CenterArea", room:getBanner("@$CenterArea")} } },
            min = 1,
            max = 1,
            prompt = "#rmt__zhanwang_getCenter",
            skill_name = zhanwang.name
          })
          if #get > 0 then
            room:moveCardTo(get, Player.Hand, player, fk.ReasonJustMove, zhanwang.name, "", true, player)
          end
        elseif mark == "DrawPileTop" then
          if #room.draw_pile > 0 then
            local cards = room:getNCards(1)
            if #cards > 0 then
              player.room:obtainCard(player, cards, false, fk.ReasonJustMove)
            end
          end
        elseif mark == "CardInBoard" then
          local targets = table.filter(room.alive_players, function(p)
            return #p:getCardIds("ej") > 0
          end)
          if #targets > 0 then
            local to = room:askToChoosePlayers(player, {
              min_num = 1,
              max_num = 1,
              targets = targets,
              skill_name = zhanwang.name,
              prompt = "#rmt__zhanwang_getInBoard",
              cancelable = false,
            })[1]
            local card = room:askToChooseCard(player, {
              target = to,
              flag = "ej",
              prompt = "#rmt__zhanwang_getInBoard",
              skill_name = zhanwang.name,
            })
            room:moveCardTo({card}, Player.Hand, player, fk.ReasonJustMove, zhanwang.name, "", true, player)
          end
        else
          if player:getMark("rmt__zhanwang_areatoplayer"..loop) ~= 0 and 
          not room:getPlayerById(player:getMark("rmt__zhanwang_areatoplayer"..loop)):isAllNude() then
            local get = room:askToChooseCards(player, {
              target = room:getPlayerById(player:getMark("rmt__zhanwang_areatoplayer"..loop)),
              flag = "hej",
              min = 1,
              max = 1,
              prompt = "#rmt__zhanwang_getPlayer::"..player:getMark("rmt__zhanwang_areatoplayer"..loop),
              skill_name = zhanwang.name
            })
            if #get > 0 and not table.contains(player:getCardIds("h"), get[1]) then
              room:moveCardTo(get, Player.Hand, player, fk.ReasonJustMove, zhanwang.name, "", true, player)
            end      
          end
        end
      end
    end
  end,
})

zhanwang:addEffect(fk.Damage, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(zhanwang.name) then return false end
    if target == player then
      local can = true
      for loop = 1, 30, 1 do
        if player:getMark("rmt__zhanwang_areatoplayer"..loop) == data.to.id then
          can = false
          break
        end
      end
      return can
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    for loop = 1, 30, 1 do
      local mark = player:getMark("rmt__zhanwang_area"..loop)
      if mark ~= 0 then
        table.insert(choices, "第"..loop.."个："..Fk:translate(mark))
      end
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      min_num = 1,
      max_num = 1,
      skill_name = zhanwang.name,
      prompt = "#rmt__zhanwang-choice::"..data.to.id,
      cancelable = false,
    })
    if #choice > 0 then
      for loop = 1, 30, 1 do
        local mark = player:getMark("rmt__zhanwang_area"..loop)
        if choice == "第"..loop.."个："..Fk:translate(mark) then
          player.room:setPlayerMark(player, "rmt__zhanwang_area"..loop, data.to.general) --这个方便动态描述直接调用
          player.room:setPlayerMark(player, "rmt__zhanwang_areatoplayer"..loop, data.to.id)
          break
        end
      end
    end
  end,
})

zhanwang:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
  player.room:setPlayerMark(player, "rmt__zhanwang_area"..1, "CenterArea")
  player.room:setPlayerMark(player, "rmt__zhanwang_area"..2, "DrawPileTop")
  player.room:setPlayerMark(player, "rmt__zhanwang_area"..3, "CardInBoard")
end)

zhanwang:addLoseEffect(function (self, player, is_death)
  for loop = 1, 30, 1 do
    player.room:setPlayerMark(player, "rmt__zhanwang_areatoplayer"..loop, 0)
    player.room:setPlayerMark(player, "rmt__zhanwang_area"..loop, 0)
  end
end)

return zhanwang
